package service

import (
	"context"
	"errors"
	"go.uber.org/zap"
	"seichat/biz"
	"seichat/dao"
	"seichat/models"
	"seichat/types"
)

type RelationService interface {
	AddFriend(ctx context.Context, req types.AddFriendReq) error
	GetFriendListByUserId(ctx context.Context, userId uint) ([]*models.User, error)
	AddFriendByUserName(ctx context.Context, req types.AddFriendByUserNameReq) error
	GetRelationListByOwnerId(ctx context.Context, ownerId uint, relationType int) ([]*models.Relation, error)
	FindRelationByUserIdAndTargetId(ctx context.Context, ownerId, targetId uint, relationType int) (*models.Relation, bool)
	FindUsersIdByGroupId(ctx context.Context, groupId uint) (*[]uint, error)
}
type RelationServiceImpl struct {
	relationDao dao.RelationDao
	userSvc     UserService
}

func (r *RelationServiceImpl) FindUsersIdByGroupId(ctx context.Context, groupId uint) (*[]uint, error) {
	relations, err := r.relationDao.FindUsersIdByGroupId(ctx, groupId)
	if err != nil {
		return nil, err
	}
	userIDs := make([]uint, 0)
	for _, v := range relations {
		userId := v.OwnerId
		userIDs = append(userIDs, userId)
	}
	return &userIDs, nil
}

func (r *RelationServiceImpl) GetRelationListByOwnerId(ctx context.Context, ownerId uint, relationType int) ([]*models.Relation, error) {
	listByOwnerId, err := r.relationDao.GetRelationListByOwnerId(ctx, ownerId, relationType)
	if err != nil {
		return nil, err
	}
	return listByOwnerId, nil
}

func (r *RelationServiceImpl) FindRelationByUserIdAndTargetId(ctx context.Context, ownerId, targetId uint, relationType int) (*models.Relation, bool) {
	return r.relationDao.FindRelationByUserIdAndTargetId(ctx, ownerId, targetId, relationType)
}

// AddFriend 添加好友
func (r *RelationServiceImpl) AddFriend(ctx context.Context, req types.AddFriendReq) error {
	targetUser, err := r.userSvc.FindUserById(ctx, req.TargetId)
	if err != nil {
		return biz.UserNotExist
	}
	if targetUser.ID == 0 {
		zap.S().Info("未查询到用户")
		return biz.UserNotExist
	}
	err = r.relationDao.AddFriend(ctx, req.UserId, req.TargetId)
	if err != nil {
		return err
	}
	return nil
}

// AddFriendByUserName 通过用户名添加好友
func (r *RelationServiceImpl) AddFriendByUserName(ctx context.Context, req types.AddFriendByUserNameReq) error {
	//1. 校验当前用户是否存在
	_, err := r.userSvc.FindUserById(ctx, req.UserId)
	if err != nil {
		return biz.UserNotExist
	}
	//2. 校验添加的用户是否存在
	targetUser, err := r.userSvc.FindUserByName(ctx, req.TargetName)
	if errors.Is(err, biz.UserNotExist) {
		return err
	}
	err = r.relationDao.AddFriend(ctx, req.UserId, targetUser.ID)
	if err != nil {
		return err
	}
	return nil
}

func (r *RelationServiceImpl) GetFriendListByUserId(ctx context.Context, userId uint) ([]*models.User, error) {
	list, err := r.relationDao.FindFriendListByUserId(ctx, userId, 1)
	if err != nil {
		return nil, err
	}
	if len(list) == 0 {
		return nil, errors.New("好友为空")
	}
	return list, nil
}

func NewRelationService(relationDao dao.RelationDao, userSvc UserService) RelationService {
	return &RelationServiceImpl{
		relationDao: relationDao,
		userSvc:     userSvc,
	}
}
